﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Web.SessionState;
using ExpertMIS.Brokers;
using ExpertMIS.Entities;

namespace ExpertMIS
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class RegisterAction : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/json";
            string errorInfo = string.Empty;

            ADMUserBroker broker = new ADMUserBroker();
            ADMUserEntity user = new ADMUserEntity();

            user.Account = context.Request["account"];
            user.Email = "";
            user.Enable = true;
            user.IsAdmin = false;
            user.IsExpert = false;
            user.IsPassed = true;
            user.Name = context.Request["account"];
            user.Password = context.Request["passwd"];
            user.Tel = "";

            if (!broker.RegisterUser(user, ref errorInfo))
            {
                context.Response.Write("{error:true,text:\"" + errorInfo.Replace("\"", "'") + "\"}");
            }
            else
            {
                context.Response.Write("{error:false,url:\"" + HttpUtility.HtmlEncode("Workships/Default.aspx") + "\"}");
                ExpertMIS.Codes.Utility.GetEnvironment().UserLogon(user);
            }

            context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
